package com.example.bluetooth.le;

import java.util.ArrayList;
import java.util.List;
import com.brtbeacon.sdk.BRTBeacon;
import com.brtbeacon.sdk.BRTBeaconManager;
import com.brtbeacon.sdk.BRTThrowable;
import com.brtbeacon.sdk.callback.BRTBeaconManagerListener;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Beacon扫描
 */
public class TestRangingActivity extends ListActivity {
    BRTBeaconManager beaconManager;
    private LeDeviceListAdapter mLeDeviceListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        BleApplication app = (BleApplication) getApplication();
        beaconManager = app.getBRTBeaconManager();// V3>>EF:19:00:E1:4D:F9
                                                  // //v1>>C1:70:49:78:DA:62 >
        // beaconManager.setRegion(new BRTRegion("brt", null,
        // "CE:74:A6:B3:4D:98", null, null));
        beaconManager
                .setBRTBeaconManagerListener(new BRTBeaconManagerListener() {

                    @Override
                    public void onUpdateBeacon(
                            final ArrayList<BRTBeacon> beacons) {
                        // 回调是在非主线程执行的，所以建议操作ui线程的代码放在runOnUiThread或者Handler中执行；
                        runOnUiThread(new Runnable() {
                            public void run() {
                                mLeDeviceListAdapter.addDeviceAll(beacons);
                                mLeDeviceListAdapter.notifyDataSetChanged();
                            }
                        });

                    }

                    @Override
                    public void onNewBeacon(final BRTBeacon beacon) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                // System.out.println("onNewBeacon>>>>>>" +
                                // beacon.getMacAddress());
                            }
                        });
                    }

                    @Override
                    public void onGoneBeacon(final BRTBeacon beacon) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                // System.out.println("onGoneBeacon>>>>>>" +
                                // beacon.getMacAddress());
                            }
                        });
                    }

                    @Override
                    public void onError(BRTThrowable throwable) {

                    }

                });
    }

    @Override
    protected void onResume() {
        beaconManager.startRanging();
        mLeDeviceListAdapter = new LeDeviceListAdapter();
        setListAdapter(mLeDeviceListAdapter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        beaconManager.stopRanging();
        mLeDeviceListAdapter.clear();
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        beaconManager.stopRanging();
        super.onBackPressed();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        final BRTBeacon device = mLeDeviceListAdapter.getDevice(position);
        if (device == null)
            return;
        final Intent intent = new Intent(this, TestControlActivity.class);
        intent.putExtra("TAG", device);
        intent.putExtra(TestControlActivity.EXTRAS_DEVICE_NAME,
                device.getName());
        intent.putExtra(TestControlActivity.EXTRAS_DEVICE_ADDRESS,
                device.getMacAddress());
        if (beaconManager != null) {
            beaconManager.stopRanging();
        }
        startActivity(intent);
    }

    // Adapter for holding devices found through scanning.
    private class LeDeviceListAdapter extends BaseAdapter {
        private ArrayList<BRTBeacon> mLeDevices;
        private LayoutInflater mInflator;

        public LeDeviceListAdapter() {
            super();
            mLeDevices = new ArrayList<BRTBeacon>();
            mInflator = TestRangingActivity.this.getLayoutInflater();
        }

        public void addDeviceAll(List<BRTBeacon> device) {
            mLeDevices.clear();
            mLeDevices.addAll(device);
        }

        public BRTBeacon getDevice(int position) {
            return mLeDevices.get(position);
        }

        public void clear() {
            mLeDevices.clear();
        }

        @Override
        public int getCount() {
            return mLeDevices.size();
        }

        @Override
        public Object getItem(int i) {
            return mLeDevices.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            // General ListView optimization code.
            if (view == null) {
                view = mInflator.inflate(R.layout.listitem_device, null);
                viewHolder = new ViewHolder();
                viewHolder.deviceAddress = (TextView) view
                        .findViewById(R.id.device_address);
                viewHolder.deviceName = (TextView) view
                        .findViewById(R.id.device_name);
                viewHolder.deviceRssi = (TextView) view
                        .findViewById(R.id.device_rssi);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            BRTBeacon device = mLeDevices.get(i);
            final String deviceName = device.getName();

            viewHolder.deviceName.setText(
                    String.format("%s  uuid:%s", deviceName, device.getUuid()));
            viewHolder.deviceAddress.setText(String.format(
                    "%s major:%d minor:%d", device.getMacAddress(),
                    device.getMajor(), device.getMinor()));
            viewHolder.deviceRssi.setText(device.getRssi() + "");

            return view;
        }
    }

    static class ViewHolder {
        TextView deviceRssi;
        TextView deviceName;
        TextView deviceAddress;
    }

}
